#{extends '/main.html'/}
<style>
#listTable {
	width: 100%;
}
</style>
<div id="main">
	<table id="listTable" cellspacing="0" cellpadding="0">
		<tr>
			<th width="150">名称(点击查看详情)</th>
			<th>类型</th>
			<th>描述</th>
			<th>市场价</th>
			<th>销售价</th>
			<th>创建时间</th>
			<th>修改时间</th>
			<th>图片</th>
			<th>操作</th>
		</tr>
		<tbody id="listBody">
		</tbody>
	</table>
	<div id="pagination">
		共<span style="color: red;" id="totalNum"></span>个商品 | 每页显示 <select
			id="num">
			<option value="10">10</option>
			<option value="20">20</option>
			<option value="30">30</option>
			<option value="40">40</option>
			<option value="50">50</option>
		</select>条 | <input type="button" value="上一页" id="previous" /> 第<span
			style="color: red;" id="page"></span>页<input type="button"
			value="下一页" id="next" />|共<span style="color: red;" id="pageCount"></span>页
	</div>
	<script type="text/javascript">
		var page = 1;
		var num = 10;
		var pageCount = 1;
		var totalNum = 0;
		
		//其他页面操作弹出消息提示
		var tips = '${tips}';
		if(tips.length>0){
			alert(tips);
		}

		function getData() {
			num = $("#numPerPage").val();
			$
					.getJSON(
							'@{manager.ShopItemController.getList()}',
							{
								"page" : page,
								"nun" : num
							},
							function(data) {
								//设置参数
								page = data.page;
								num = data.numPerPage;
								pageCount = data.pageCount;
								totalNum = data.totalNum;

								//渲染
								$("#totalNum").text(totalNum);
								$("#page").text(page);
								$("#pageCount").text(pageCount);
								$("#previous").attr("disabled",
										page == 1 ? "disabled" : "");
								$("#next").attr("disabled",
										page == pageCount ? "disabled" : "");
								//每页条数
								var options = $("#num option").children();
								for ( var i = 0; i < options.length; i++) {
									if (num = options[i].value) {
										options[i].selected = true;
										break;
									}
								}

								var html = "";
								for ( var i = 0; i < data.itemList.length; i++) {
									var desc = data.itemList[i].description;
									var tr = "<tr id='"+data.itemList[i].itemId+"'>"
											+ "<td><a href='"+data.itemList[i].detailUrl+"' target='_blank'>"
											+ data.itemList[i].itemName
											+ "</a></td>"
											+ "<td>"
											+ (data.itemList[i].type == '1' ? "男童装"
													: "女童装")
											+ "</td>"
											+ "<td>"
											+ (desc == null || desc.length == 0 ? "<span style='font-style:italic;'>无<span>"
													: desc)
											+ "</td>"
											+ "<td>"
											+ data.itemList[i].marketPrice
											+ "￥</td>"
											+ "<td>"
											+ data.itemList[i].price
											+ "￥</td>"
											+ "<td>"
											+ data.itemList[i].createTime
											+ "</td>"
											+ "<td>"
											+ data.itemList[i].modifyTime
											+ "</td>"
											+ "<td class='img'></td>"
											+ "<td><a href='@{manager.ShopItemController.toModify}?id="
											+ data.itemList[i].itemId
											+ "'>修改</a>|<a href=\"javascript:deleteItem('"
											+ data.itemList[i].itemId
											+ "')\">删除</a></td>"
									"</tr>"
									html += tr;
								}
								$("#listBody").empty();
								$("#listBody").append(html);
								$("#listBody tr")
										.each(
												function(i) {
													var img = $("<img/>");
													img
															.attr(
																	"src",
																	data.itemList[i].imagePath);
													img[0].width = 100;
													img[0].height = 100;
													img
															.css("cursor",
																	"pointer");
													img.attr("title", "点击查看大图");
													var link = $("<a href='"+data.itemList[i].imagePath+"'></a>");
													link.attr("target",
															"_blank");
													link.append(img);
													$(this).children()[7]
															.appendChild(link[0]);
												});
								$("#listTable td").css("border",
										"1px solid #89afEF");
								$("#listTable th").css("border",
										"1px solid #89afEF");
								window.parent.autoFrameSize();
							});
		}

		function deleteItem(id) {
			if (!confirm("确定删除此商品？删除后将不可恢复!")) {
				return;
			}
			$.ajax({
				"url" : "@{manager.ShopItemController.delete()}",
				"type" : "POST",
				"data" : "id=" + id,
				"success" : function(data) {
					alert("删除成功");
					$("#" + id).remove();
				}
			});
		}
		$("#num").change(function() {
			getData();
		});
		$("#previous").click(function() {
			page--;
			getData();
		});
		$("#previous").click(function() {
			page++;
			getData();
		});

		getData();
	</script>
</div>